/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle
 * @since : 2015-04-29 22:36:07
 * @filename : src/public/components/moduleTA/moduleTA.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module) {
    
    var moduleTA = function() {
        
        this.action = function(module,type) {
            if (module) {this.doTA(module,type)};
        };

        this.doTA = function(moduleInfo,type) {
            switch(moduleInfo.type) {
               case 'defaults':
                   this.defaults(moduleInfo,type);
               break;
               case 'slide':
                   this.slider(moduleInfo,type);
               break;
               default:
                   this.defaults(moduleInfo,type);
            }
        };

        this.defaults = function(m,type) {
            $('.module').css('display','none');
            if (type =='current') {
                $('#'+m.moduleName).css('display','none');
            } else if (type == 'next') {
                $('#'+m.moduleName).css('display','block');
            }
        }; 

        /**
            {
              type: 动画类型
              direction: 动画方向
              offset: 动画偏移量
              speed: 动画时间
              display: true/false
              fn: callback
            }
            1:滑动需要考虑平板菜单show的情况,left不是从0开始.(手机导航可以是独立的一个模块,就不存在了）．
　　　　    2:位置偏移和transtion的css3偏移，目前css3偏移后会引起fixed定位等其他bug
        */

        this.slider = function(m,type) {
            var opt = {
                type : 'slide',
                direction: 'right',
                offset: $(window).width(),
                speed: 300,
                display: true,
                css3: false
            };

            function css3(m,move,opt) {
                $('#'+m.moduleName).css({
                    'left': move+'px',
                    'transition': opt.speed+'ms linear',
                    '-webkit-transition': opt.speed+'ms linear', 
                    '-moz-transition': opt.speed+'ms linear'
                });
            };

            //todo.....ipad可以从margin着手处理  transtion的偏移量计算,也可以调整时间，进来的速度比出去快，仿ios效果
            opt = _.extend(opt,m);  
            if (type == 'current') {
                if (opt.direction == 'right') {
                    if (opt.css3) {
                        setTimeout(function() {
                            css3(m,-opt.offset,opt);
                        },300);
                        setTimeout(function(){
                            if(opt.display) {$('#'+m.moduleName).hide()};
                        },opt.speed+300);
                    } else {
                        $('#'+m.moduleName).animate({'left':-opt.offset+'px'},opt.speed,function(){
                            if (opt.display) {$(this).hide()};
                        });
                    }
                } else if (opt.direction == 'left') {
                    if (opt.css3) {
                        setTimeout(function() {
                            css3(m,opt.offset,opt);
                        },300);
                        setTimeout(function(){
                            if(opt.display) {$('#'+m.moduleName).hide()};
                        },opt.speed+300);
                    } else {
                        $('#'+m.moduleName).animate({'left':opt.offset+'px'},opt.speed,function(){
                            if(opt.display) {$(this).hide()};
                        });
                    }
                }
            } else if (type == 'next') {
                $('#'+m.moduleName).css({'display':'block'});
                if (m.index) {
                    $('#moduleBox').css('overflow','visible');
                } else { 
                    if (opt.direction == 'left') {
                        $('#'+m.moduleName).css({'left':opt.offset+'px'});
                        if (opt.css3) {
                            setTimeout(function(){
                                css3(m,0,opt);
                            },250);
                            setTimeout(function(){
                                $('#moduleBox').css('overflow','visible');
                            },opt.speed+300);
                        } else { 
                            $('#'+m.moduleName).animate({'left':'0px'},opt.speed,function(){
                                $('#moduleBox').css('overflow','visible')
                            });
                        }
                        
                    } else if (opt.direction == 'right') {
                        $('#'+m.moduleName).css({'left':-opt.offset+'px'});
                        if (opt.css3) {
                            setTimeout(function(){
                                css3(m,0,opt);
                            },250);
                            setTimeout(function(){
                                $('#moduleBox').css('overflow','visible');
                            },opt.speed+300);
                        } else {
                            $('#'+m.moduleName).animate({'left':'0px'},opt.speed,function(){
                                $('#moduleBox').css('overflow','visible')
                            });
                        }
                    } 
                }
            }
        }; 
    };

    module.exports = new moduleTA();
});
